var express = require('express');
const { getProducts,deleteProducts,modifyProducts,addProducts,getProductsall} = require("../service/productsService");
var router = express.Router();
const {uploadFiles}=require('../util/handleFiles.js');
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
//获取保险产品数据
router.get("/getProducts", async function (req, res, next) {
  
  const data = await getProducts(req.query);
  res.send(data);
  // console.log(111111,data);
});
//删除某个保险数据 by_id
router.post('/deleteProducts', async function (req, res, next) {
 
  const data=await deleteProducts(req.body);
  res.send(data);
});
//修改某个保险数据
router.post('/modifyProducts',async function(req, res, next) {
  const data=await modifyProducts(req.body);
  res.send(data);
});
//新增保险产品信息
router.post('/addProducts',async function(req, res, next) {
  const data=await addProducts(req.body);
  res.send(data);
});
//获取全部保险产品
router.get('/getProductsall',async function(req, res, next) {
  const data=await getProductsall(req.query);
  res.send(data);
});
// 图片上传
router.post('/uploadImages',  function(req, res, next) {
  const uploadImages=uploadFiles(
        {
               path:'./public/temp',
              key:'file',
              // size:5000
        }
  );
  uploadImages(req,res,err=>{
        if(err){
              console.log('图片加载失败--------');
              console.log(err);
        }else{
              console.log('图片加载成功');
              res.send({
                    message:'图片上传成功',
                    status:1,
                    data:req.files.map(item=>item.filename)
              })
        }
  })
});
module.exports = router;
